package com.weakteam.orderonline.dataresource.dao;

import com.weakteam.orderonline.dataresource.domain.Place;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @author Nail
 */

@Repository
public class PlaceDAOImpl implements PlaceDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<Place> getAll() {
        return em.createQuery("FROM Place p").getResultList();
    }

    @Override
    public void save(Place place) {
        this.em.merge(place);
    }

    @Override
    public boolean delete(Integer id) {
        final String query = "DELETE Place p WHERE p.id = :id";
        int count = em.createQuery(query).setParameter("id", id).executeUpdate();
        return (count > 0);
    }

}
